--
-- Author: shunguo.chen
-- Date: 2017-12-27 18:08:58
--
-- PvpMatchView pvp匹配界面

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.PvpMatchScene

local REST_TIME_STR = L("NumberofSeconds")

function ClassRef:init()
	self._ui.Button_quxiao:addClickEventListener(function(sender)
			display.setBtnTouchEnable(sender, false)
			executeFunc(self._onOperate, "cancel", self)
		end)

	local con = self._ui.Panel_ani
	local di = con:getChildByName("di")
	local mask = con:getChildByName("mask")
	mask:retain()
	mask:removeFromParent()
	di:retain()
	di:removeFromParent()

	local clipNode = cc.ClippingNode:create()
	clipNode:setContentSize(con:getContentSize())
	clipNode:setInverted(false)
	clipNode:setAlphaThreshold(0.5)
	clipNode:setStencil(mask)
	clipNode:addChild(di)
	clipNode:setName("di")
	con:addChild(clipNode, -1)

	di:release()
	mask:release()
end

function ClassRef:show()
	self:setVisible(true)
	self:openAction()
	self:performWithDelay(function()
		self:playAction("animation", true)
	end, 0.5)
end

function ClassRef:refresh(maxSeconds, curSeconds, canCancel)
	self._secondsMax = maxSeconds
	self._seconds = curSeconds or 1
	self._initTime = app:getTime()
	if self._act then
		self:stopAction(self._act)
		self._act = nil
	end
	self:setRestText()
	display.setBtnTouchEnable(self._ui.Button_quxiao, canCancel)
end

function ClassRef:setTipContent(str)
	self._ui.tips:setString(str)
end

function ClassRef:setRestText()
	local seconds = self._seconds + math.floor(app:getTime() - self._initTime)
	if seconds <= self._secondsMax then
		self._ui.time:setString(string.format(REST_TIME_STR, seconds))
		self._act = self:performWithDelay(function()
				self:setRestText()
			end, 1)
	else
		self._act = nil
		self:closeView()
	end
end


return ClassRef